#region Using directives
using System;
using Microsoft.DirectX.Direct3D;
using System.Collections.Generic;
using SharpWind;
#endregion

namespace SharpWind.Graphics.Settings
{
	public class GraphicsDeviceEnumerator
	{
		#region Fields

		private static Format[] allowedFormats = new Format[]
		{ 
			Format.X1R5G5B5, 
			Format.A1R5G5B5,
			Format.R5G6B5,
			Format.X8R8G8B8,
			Format.A8R8G8B8,
			Format.A2R10G10B10
		};

		private List<DisplayMode> displayModes = new List<DisplayMode>();

		private AdapterDetails adapterDetails;

		#endregion

		#region Methods

		public static Microsoft.DirectX.Direct3D.DisplayMode DisplayModeFromSettings( SharpWind.Settings settings )
		{
			Microsoft.DirectX.Direct3D.DisplayMode displayMode = new Microsoft.DirectX.Direct3D.DisplayMode();
			displayMode.Width		= settings.Width;
			displayMode.Height		= settings.Height;
			displayMode.RefreshRate = settings.RefreshRate;
			displayMode.Format		= allowedFormats[( settings.BitsPerPixel / 32 ) * 3];

			return displayMode;
		}

		public void Enumerate()
		{
			this.adapterDetails = Manager.Adapters.Default.Information;

			for( int i = 0; i < allowedFormats.Length; i++ )
			{
				foreach( Microsoft.DirectX.Direct3D.DisplayMode dm in Manager.Adapters[0].SupportedDisplayModes )
				{
					if( dm.Height >= 480 )
					{
						DisplayMode displayMode = new DisplayMode();
						displayMode.Mode = dm;

						if( i < 3 )
						{
							displayMode.BitsPerPixel = 16;
						}
						else
						{
							displayMode.BitsPerPixel = 32;
						}

						this.displayModes.Add( displayMode );
					}
				}
			}
		}

		#endregion

		#region Properties

		public List<DisplayMode> DisplayModes
		{
			get
			{
				return this.displayModes;
			}
		}

		public AdapterDetails AdapterDetails
		{
			get
			{
				return this.adapterDetails;
			}
		}

		#endregion
	}
}
